package SQL.abysnSQL;
import java.util.HashMap;

import heap.ResultRow;
import heap.ResultSchema;
import SQL.query.SQL;
import SQL.valueUtil.BoolExprVerify;
import SQL.valueUtil.ValueComparator;
import Transaction.Transaction;


public class BoolCondition {
	public BoolCondition leftB,rightB;
	public Value leftV,rightV;
	public Cop cop;
	public SQL subquery;
	public TYPE type;
	
	public static enum TYPE{
		VALUE,AND,OR,EXIST,NOTEXIST,ANY,IN,ALL
	}
/**
 * constructors	
 * 	bool-expr ::= value cop value
                 | bool-expr AND bool-exp
				 | bool-expr OR bool-exp
				 | [NOT] EXISTS (subquery)
				 | value cop ANY (supquery)
				 | value IN (subquery)
				 | value op ALL (subquery)

 */
	public BoolCondition(Value l,Cop op, Value r){
		leftV = l;
		cop = op;
		rightV = r;
		type = TYPE.VALUE;
	}
	
	public BoolCondition(SQL s,boolean ex){
		subquery = s;
		if(ex)
			type = TYPE.EXIST;
		else
			type = TYPE.NOTEXIST;
	}
	

	public BoolCondition(Value v,Cop o,SQL s,boolean all){
		leftV = v;
		cop = o;
		subquery = s;
		if(all)
			type = TYPE.ALL;
		else
			type = TYPE.ANY;
		}
	
	public BoolCondition(Value v,SQL s){
		leftV = v;
		subquery = s;
		type = TYPE.IN;
	}
	
	public BoolCondition(BoolCondition l, BoolCondition r,boolean and){
		leftB = l;
		rightB = r;
		if(and)
			type = TYPE.AND;
		else
			type = TYPE.OR;
	}
	/**
	 * check the correctness of boolean operation
	 * @param schema
	 * @return
	 */
	public boolean validation(ResultSchema schema){
		switch(type){
		case VALUE:
		{
			boolean l = leftV.validation(schema);
			boolean r = rightV.validation(schema);
			if(l && r && (
				( leftV.getColName() != null
				&& leftV.constvalue != null ) ||
				 rightV.getColName() != null
				&& rightV.constvalue != null ))
				return true;
			return false;
		}
		case AND:
		{

			boolean l = leftB.validation(schema);
			boolean r = rightB.validation(schema);
			return l && r;
		}
		case OR:
		{
			boolean l = leftB.validation(schema);
			boolean r = rightB.validation(schema);
			return l || r;
		}
		case ANY:
		case ALL:
		case IN:
		{
			//check one-row~
		}
		}	
		return false;
	}
}
